using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FizzyBubblesController : MonoBehaviour
{
	private Liquid liquid;

	private Vector3 startPosition;

	private Vector3 maxPosition;

	private Vector3 graveyardPosition = new Vector3(1000f, 1000f, -1000f);

	private List<Transform> objectsPool;

	private List<Transform> graveyard;

	private int targetObjectsInPool = 5;

	private Sprite bubble_1;

	private Sprite bubble_2;

	private Coroutine makingBubbles;

	private void Awake()
	{
		liquid = GetComponent<Liquid>();
		maxPosition = startPosition + Vector3.up * liquid.GetRawHeight();
		bubble_1 = Resources.Load<Sprite>("Sprites/Bubbles/bubble1");
		bubble_2 = Resources.Load<Sprite>("Sprites/Bubbles/bubble2");
	}

	private void PopulateGraveyard()
	{
		objectsPool = new List<Transform>();
		graveyard = new List<Transform>();
		GameObject original = Resources.Load("Prefabs/Liquids/FizzyBubble") as GameObject;
		for (int i = 0; i < 15; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(original, graveyardPosition, default(Quaternion));
			graveyard.Add(gameObject.transform);
		}
	}

	public void CheckIfLiquidIsFizzy(LiquidType liquidType)
	{
		if (LiquidTypes.IsFizzy(liquidType))
		{
			PopulateGraveyard();
			makingBubbles = StartCoroutine(MakeBubbles());
		}
		else
		{
			UnityEngine.Object.Destroy(this);
		}
	}

	private void Update()
	{
		if (liquid != null)
		{
			startPosition = liquid.transform.position;
			maxPosition = startPosition + Vector3.up * liquid.GetRawHeight();
		}
	}

	public void StopMakingBubbles()
	{
		if (makingBubbles != null)
		{
			StopCoroutine(makingBubbles);
		}
	}

	private IEnumerator MakeBubbles()
	{
		while (true)
		{
			float randomTime = UnityEngine.Random.Range(0.04f, 0.2f);
			yield return new WaitForSeconds(randomTime);
			Transform bubble = graveyard[0];
			graveyard.RemoveAt(0);
			objectsPool.Add(bubble);
			bubble.position = CalculateRandomStartPosition();
			bubble.gameObject.AddComponent<FizzyBubble>();
			FizzyBubble fizzyBubbleComponent = bubble.GetComponent<FizzyBubble>();
			fizzyBubbleComponent.SetBubblesControllerConnection(this);
			fizzyBubbleComponent.isVisible = true;
			fizzyBubbleComponent.SetMaxPosition(maxPosition);
			fizzyBubbleComponent.SetSprite(GetBubbleRandomSprite());
		}
	}

	private Vector3 CalculateRandomStartPosition()
	{
		Vector3 result = default(Vector3);
		float rawWidth = liquid.GetRawWidth();
		result.x = UnityEngine.Random.Range(startPosition.x - rawWidth * 0.5f, startPosition.x + rawWidth * 0.5f);
		result.y = startPosition.y;
		result.z = startPosition.z;
		return result;
	}

	private Sprite GetBubbleRandomSprite()
	{
		float num = UnityEngine.Random.Range(0f, 1f);
		if (num > 0.5f)
		{
			return bubble_1;
		}
		return bubble_2;
	}

	public void SendBubbleToGraveyard(Transform bubble)
	{
		objectsPool.Remove(bubble);
		graveyard.Add(bubble);
		bubble.position = graveyardPosition;
	}

	public void OnDisable()
	{
		if (objectsPool != null)
		{
			foreach (Transform item in objectsPool)
			{
				if (item != null)
				{
					UnityEngine.Object.Destroy(item.gameObject);
				}
			}
		}
		if (graveyard != null)
		{
			foreach (Transform item2 in graveyard)
			{
				if (item2 != null)
				{
					UnityEngine.Object.Destroy(item2.gameObject);
				}
			}
		}
	}
}
